import 'dart:io';

import 'package:flustars/flustars.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:smart_freezer/core/resources/app_config.dart';
import 'package:smart_freezer/core/resources/icon_names.dart';
import 'package:smart_freezer/core/resources/routers.dart';
import 'package:smart_freezer/core/utils/custom_floating_button_location.dart';
import 'package:smart_freezer/core/utils/lc_event_bus/notify.dart';
import 'package:smart_freezer/core/utils/local_storage.dart';
import 'package:smart_freezer/ui.pages/home/home_content_page.dart';

import '../../core/utils/lc_event_bus/call.dart';

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {

  bool isDarkMode = LocalStorage.getThemeMode() == 1;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    Call.addCallBack(Notify.dark_mode, (data){
      setState((){});
      isDarkMode = LocalStorage.getThemeMode() == 1;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        // backgroundColor: Colors.white,
        titleSpacing: 8,  // 左右间距
        automaticallyImplyLeading: false,
        elevation: 2,
        title: Container(
          width: MainCommon.screenWidth,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Container(
                child: Image.asset(isDarkMode ? IconNames.home_logo_hei : IconNames.home_logo, height: 28,),
              ),

              Row(
                children: [
                  InkWell(
                      onTap: (){
                        // 检测蓝牙权限和定位权限
                        checkPermission();
                      },
                      child: Container(
                          padding: EdgeInsets.fromLTRB(10, 0, 15, 0),
                          child: Icon(Icons.wifi, color: Colors.grey,)
                      )
                  ),
                  InkWell(
                      onTap: () {
                        Get.toNamed(LCRouter.home_setting)?.then((value) {
                          setState((){});
                          isDarkMode = LocalStorage.getThemeMode() == 1;
                        });
                      },
                      child: Image.asset(IconNames.home_sz, height: 30,)
                  ),
                ],
              ),
            ],
          ),
        ),
      ),

      body: HomeContentPage(),
      floatingActionButton: Container(
        height: 70,
        child: InkWell(
          child: Image.asset(isDarkMode ? IconNames.home_add02 : IconNames.home_add),
          onTap: () {
            Get.toNamed(LCRouter.qrcode);
          },
        ),
      ),
      floatingActionButtonLocation: CustomFloatingActionButtonLocation(FloatingActionButtonLocation.endDocked, -10, -20),
    );
  }

  // 检测定位权限是否开启
  Future<void> checkPermission() async {
    // undetermined未定, granted被授予, denied被拒绝, restricted受限制的, permanentlyDenied永久拒绝
    if (Platform.isAndroid || Platform.isIOS) {
      final isLocationStatus = await Permission.location.request().isGranted;
      final isBLEStatus = await Permission.bluetooth.request().isGranted;

      if(!isLocationStatus){
        SmartDialog.showToast("app need location permission");
        return;
      }
      if(!isBLEStatus){
        SmartDialog.showToast("app need bluetooth permission");
        return;
      }

      Get.toNamed(LCRouter.wifi_page);
    }
  }


}
